package com.gusto.engine.spring.serviceparsers;

import java.util.List;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import com.gusto.engine.semsim.service.impl.SimilarityServiceImpl;

public class ParserSimilarityService extends AbstractBeanDefinitionParser {
		
	@SuppressWarnings("unchecked")
	protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
		BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(SimilarityServiceImpl.class);
		
		List cacheSize = DomUtils.getChildElementsByTagName(element, "cache-size");
		if (cacheSize.size() == 1) {
			Element cacheSizeElement = (Element)cacheSize.get(0);
			beanDefinitionBuilder.addPropertyValue("CACHE_SIZE", cacheSizeElement.getTextContent());
		}
		
		List configFile = DomUtils.getChildElementsByTagName(element, "config-file");
		if (configFile.size() == 1) {
			Element configFileElement = (Element)configFile.get(0);
			beanDefinitionBuilder.addPropertyValue("CONFIG_FILE", configFileElement.getTextContent());
		}
		
		return beanDefinitionBuilder.getBeanDefinition();
	}
		
}
